package com.atguigu.gmall.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 小兰
 * @Date: 2023/4/25 18:40
 */
//全局自定义线程池配置
    @Configuration
public class ThreadPoolConfig {
        @Bean
    public ThreadPoolExecutor threadPoolExecutor(){
            //动态获取服务器核数
            int processors = Runtime.getRuntime().availableProcessors();
            ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(
                    processors*2,//核心线程个数 io:2n ,cpu: n+1  n:内核数据
                    processors*2,//最大线程数
                    0,//非核心线程死亡时间
                    TimeUnit.SECONDS,//时间单位
                    new ArrayBlockingQueue<>(20),//阻塞队列
                    Executors.defaultThreadFactory(),//默认创建线程工厂
                    new ThreadPoolExecutor.AbortPolicy()//拒绝策略
            );
            //  返回线程池对象
            return threadPoolExecutor;
        }
}
